<script lang="ts" setup>
import { useVbenModal } from '@vben/common-ui';
import { useVbenForm } from '#/adapter/form';
import { addWell } from '#/api/gis/well';
import type { AddWellParams } from '#/api/gis/well';
import { message } from 'ant-design-vue';

const [AddModal, AddmodalApi] = useVbenModal({
  // onCancel() {
  //   EditmodalApi.close();
  // },
  // onConfirm() {
  //   console.log('onConfirm');
  // },
});

const [BaseForm] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
  },
  handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入井编号',
      },
      fieldName: 'wellCode',
      label: '井编号：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入井名称',
      },
      fieldName: 'wellName',
      label: '井名称：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入经度',
      },
      fieldName: 'longitude',
      label: '经度：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入纬度',
      },
      fieldName: 'latitude',
      label: '纬度：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入井深',
      },
      fieldName: 'depth',
      label: '井深(米)：',
      suffix: () => '米',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '冻结',
            value: 0,
          },
          {
            label: '正常',
            value: 1,
          },
        ],
      },
      fieldName: 'status',
      label: '状态：',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入备注',
      },
      fieldName: 'remark',
      label: '备注：',
    },
  ],
});

// 定义一个自定义事件
const emit = defineEmits(['refreshTable']);
function onSubmit(values: Record<string, any>) {
  const params = values as AddWellParams;
  params.location = `POINT(${params.longitude} ${params.latitude})`;
  addWell(params).then(() => {
    AddmodalApi.close();
    message.success('添加油水井成功');
    emit('refreshTable'); // 触发自定义事件
  }).catch((error) => {
    console.error('添加油水井失败:', error);
    message.error('添加油水井失败');
  });
}
</script>

<template>
  <AddModal title="添加油水井">
    <BaseForm />
    <template #prepend-footer>
    </template>
  </AddModal>
</template>
